package com.starsea.book.ui.newpage.activity

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.BarUtils
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.base.base.widget.view.SearchTagView
import com.starsea.book.databinding.ActivitySearchBinding
import com.starsea.book.ui.newpage.adapter.SearchBookRanksAdapter
import com.starsea.book.ui.newpage.viewmodel.HomeViewModel
import com.starsea.book.ui.newpage.weidget.FlowLayout
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-08-18 18 GMT+8
 * @desc :
 */
class SearchActivity : VMBaseActivity<ActivitySearchBinding, HomeViewModel>() {

    lateinit var mAdapter: SearchBookRanksAdapter

    override val viewModel: HomeViewModel by viewModels<HomeViewModel>()
    override val binding: ActivitySearchBinding by viewBinding(ActivitySearchBinding::inflate)

    override
    fun onActivityCreated(savedInstanceState: Bundle?) {
        BarUtils.addMarginTopEqualStatusBarHeight(binding.llTitle)

        mAdapter = SearchBookRanksAdapter(this)
        binding.rvSearchRanks.layoutManager = LinearLayoutManager(this)

        binding.rvSearchRanks.adapter = mAdapter
        binding.ivBack.setOnClickListener { finish() }
        binding.rtvSearch.setOnClickListener {
            val keyWord = binding.etContent.text.toString()
            if (TextUtils.isEmpty(keyWord)) {
                toastOnUi("请输入搜索关键字")
                return@setOnClickListener
            }
//            viewModel.getHistorySearchKeyWord()
            startActivity<SearchResultActivity>("keyWord" to keyWord)
        }

        binding.flSearchHis.removeAllViews()
        binding.flSearchHis.setGravity(FlowLayout.START)

        binding.flSearchHot.removeAllViews()
        binding.flSearchHot.setGravity(FlowLayout.START)


        viewModel.hotSearchBook.observe(this) {
            if (it.status == 200) {
                mAdapter.submitList(it.data!!)
            } else {

            }
        }

        viewModel.hotSearchCategory.observe(this) {
            if (it.status == 200) {
                it.data!!.forEach {
                    val view = SearchTagView(this)
                    view.setText(it.name)
                    binding.flSearchHot.addView(view)
                }
            } else {

            }
        }

        viewModel.searchHistory.observe(this) {
            if (it.status == 200) {
                binding.flSearchHis.removeAllViews()
                binding.flSearchHis.setGravity(FlowLayout.START)

                it.data!!.forEach {
                    val view = SearchTagView(this)
                    view.setText(it)
                    binding.flSearchHis.addView(view)
                }
            } else {

            }
        }


//

    }

    override fun onResume() {
        super.onResume()
//        viewModel.getHistorySearchKeyWord()
    }

}